<?php
namespace Api\Controller;
use Think\Controller;
class CollectController extends CommonController{

        //实例化用户
        protected $currentUser;

        function _initialize() {
            parent::_initialize();

            //判断token是否存在
            $token = I('post.token');

            $userMap['token'] = $token;
            $user = $this->userModel->where($userMap)->find();

            if(isNullOrEmpty($user)) {
                $this->errorReturn('ERROR_TOKEN');
            }

            $this->currentUser = $user;

        }

//        public function lists() {
//
//            $pageNum = I('post.pageNum') ? I('post.pageNum') : C('DEFAULT_PAGE');
//
//            $list = $this->collectModel->queryListByUserId($this->currentUser["user_id"], $pageNum);
//
//            $this->returnData['data']['list'] = $list;
//
//            $this->response($this->returnData, 'json');
//
//        }

        public function createCollect() {

            //判断师傅是否存在
            $masterId = I("post.masterId");

            $master = $this->userModel->queryById($masterId);

            if(isNullOrEmpty($master)) {
                $this->errorReturn("ERROR_MASTER_NOT_FOUND");
            }

            $errorCode = $this->collectModel->createCollect($this->currentUser["user_id"]);

            $this->errorReturn($errorCode);

        }

        public function cancelCollect() {

            //判断师傅是否存在
            $masterId = I("post.masterId");

            $master = $this->userModel->queryById($masterId);

            if(isNullOrEmpty($master)) {
                $this->errorReturn("ERROR_MASTER_NOT_FOUND");
            }

            $errorCode = $this->collectModel->cancelCollect($this->currentUser["user_id"]);

            $this->errorReturn($errorCode);

        }

}